Skip to content

Comments

Fix stack overflow in merger with deeply nested SQL structures#14

Merged
datastx merged 2 commits intomainfrom
claude/fix-sqlfmt-dbt-file-L6QVz
Feb 23, 2026
Merged

Fix stack overflow in merger with deeply nested SQL structures#14
datastx merged 2 commits intomainfrom
claude/fix-sqlfmt-dbt-file-L6QVz

Conversation

@datastx
Copy link
Owner

@datastx datastx commented Feb 23, 2026

Summary

Fixed a stack overflow issue in the line merger that occurred when processing deeply nested SQL structures with many CTEs, window functions, and Jinja references. The problem was caused by infinite recursion when segment processing collapsed multiple segments back into a single segment with identical input.

Changes

  • src/merger.rs: Added a guard condition to detect and prevent infinite recursion in maybe_merge_lines(). When segment processing results in a single segment with the same number of lines as the input, the code now falls through to single-segment processing instead of recursing with identical input.

  • tests/integration_test.rs: Added test_format_dbt_deeply_nested() test case to verify the formatter can handle complex dbt models without stack overflow and that formatting is idempotent.

  • tests/fixtures/dbt_deeply_nested.sql: Added a comprehensive test fixture containing a real-world dbt model with:

    • Multiple nested CTEs (15+ levels)
    • Jinja ref() and source() calls
    • Complex window functions and QUALIFY clauses
    • UNION ALL operations
    • Deeply nested subqueries

Implementation Details

The fix detects when the merger's segment processing has reached a fixed point (single segment with unchanged line count) and prevents further recursion by directly processing that segment instead. This maintains the existing behavior for normal cases while preventing pathological recursion on deeply nested structures.

https://claude.ai/code/session_01QhNW6sYNjRhsG2Qjyut9Rm

…t SQL

The maybe_stubbornly_merge step could collapse multiple segments back into
a single segment with the same line count as the input. When this happened,
maybe_merge_lines would recurse with identical input, creating true infinite
recursion (not just deep recursion). This was triggered by complex dbt models
with many CTEs, Jinja refs/sources, QUALIFY, and window functions.

Added a guard: when segment processing produces a single segment with the
same line count as the input, fall through to merge_single_segment instead
of recursing with identical input.

https://claude.ai/code/session_01QhNW6sYNjRhsG2Qjyut9Rm
Rename dbt_pharmacy_panels.sql to dbt_deeply_nested.sql and replace all
domain-specific names (pharmacy, clinical programs, members, appointments)
with generic equivalents (categories, entities, events, spans) while
preserving the exact SQL structure that triggers the deep recursion bug.

https://claude.ai/code/session_01QhNW6sYNjRhsG2Qjyut9Rm
@datastx datastx force-pushed the claude/fix-sqlfmt-dbt-file-L6QVz branch from 446a090 to 4ca47f1 Compare February 23, 2026 16:12
@datastx datastx merged commit 2bb7775 into main Feb 23, 2026
9 checks passed
@datastx datastx deleted the claude/fix-sqlfmt-dbt-file-L6QVz branch February 24, 2026 07:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants